using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Evaluant.Uss;
using Evaluant.Uss.ObjectContext;
using System.IO;
using System.Diagnostics;

namespace NUnit.MappingGenerator
{
	[TestClass]
	public class TestMappingGenerator
	{
		public ObjectContext oc;

		public TestMappingGenerator()
		{
			Configure();
		}

		public virtual void Configure()
		{
			if (File.Exists("Model.eum.xml"))
				File.Delete("Model.eum.xml");

			//  Generate Mapping File
			Process p = new Process();
			ProcessStartInfo psi = new ProcessStartInfo();
			psi.FileName = @"..\..\..\MappingGenerator\bin\debug\Mapping.exe";
			psi.Arguments = @"/model:..\..\..\NUnit\MappingGenerator\Model.xml /out:Model.eum.xml";
			psi.CreateNoWindow = false;

			p.StartInfo = psi;

			p.Start();

			p.WaitForExit();

			ObjectService os = new ObjectService("engines.config", "MappingGenerator");
			oc = os.CreateObjectContext();
		}

		[TestInitialize]
		public void SetUp()
		{
			oc.InitializeRepository();
		}

		[TestMethod]
		public virtual void Simple()
		{
			Person p1 = new Person();
			p1.FirstName = "John";
			p1.Name = "Smith";
			p1.Age = 49;
			p1.Title = Title.Sir;
			p1.Address = new Address();
			p1.Address.City = "NY";
			p1.Address.Street = "42";
			p1.Address.PostCode = 10000;

			oc.BeginTransaction();
			oc.Serialize(p1);
			oc.CommitTransaction();

			oc.Clear();

			Person p = oc.Load<Person>(typeof(Person))[0];

			Assert.IsNotNull(p);
			Assert.AreEqual("John", p.FirstName);
			Assert.AreEqual("Smith", p.Name);
			Assert.AreEqual(49, p.Age);
			Assert.AreEqual(Title.Sir, p.Title);
			Assert.IsNotNull(p.Address);
			Assert.AreEqual("NY", p.Address.City);
			Assert.AreEqual("42", p.Address.Street);
			Assert.AreEqual(10000, p.Address.PostCode);
		}

		[TestMethod]
		public virtual void Delete()
		{
			Person p1 = new Person();
			p1.Name = "John";

			oc.BeginTransaction();
			oc.Serialize(p1);
			oc.CommitTransaction();

			oc.Clear();

			Person p = oc.Load<Person>(typeof(Person))[0];

			Assert.IsNotNull(p);
			Assert.AreEqual("John", p.Name);

			oc.BeginTransaction();
			oc.Delete(p);
			oc.CommitTransaction();

			oc.Clear();

			Assert.AreEqual(0, oc.Load<Person>(typeof(Person)).Count);
		}

		[TestMethod]
		public virtual void References()
		{
			Employee e1 = new Employee();
			e1.Name = "John";
			e1.Address = new Address();
			e1.Address.City = "Chicago";

			Employee e2 = new Employee();
			e2.Name = "Mitch";
			e2.Address = new Address();
			e2.Address.City = "Malibu";

			Chairman c1 = new Chairman();
			c1.Name = "Sam";
			c1.Address = new Address();
			c1.Address.City = "Washington D.C.";
			c1.Employees.Add(e1);
			c1.Employees.Add(e2);

			oc.BeginTransaction();
			oc.Serialize(c1);
			oc.CommitTransaction();

			oc.Clear();

			Chairman c = oc.Load<Chairman>(typeof(Chairman))[0];

			Assert.IsNotNull(c);
			Assert.AreEqual(2, c.Employees.Count);

			oc.Clear();

			Employee e = oc.Load<Employee>(typeof(Chairman), ".Employees[Address.City = 'Malibu']")[0];
			Assert.IsNotNull(e);
			Assert.AreEqual("Mitch", e.Name);
		}

		[TestMethod]
		public virtual void Inheritance()
		{
			Person p1 = new Person();
			p1.Name = "John";

			Employee e1 = new Employee();
			e1.Name = "Sam";

			Chairman c1 = new Chairman();
			c1.Name = "Mitch";

			oc.BeginTransaction();
			oc.Serialize(p1);
			oc.Serialize(e1);
			oc.Serialize(c1);
			oc.CommitTransaction();

			oc.Clear();

			Assert.AreEqual(3, oc.Load<Person>(typeof(Person)).Count);
			Assert.AreEqual(1, oc.Load<Employee>(typeof(Employee)).Count);
			Assert.AreEqual(1, oc.Load<Chairman>(typeof(Chairman)).Count);
		}
	}
}
